package com.shop2cn.iapi.prodstockmgr.testcase;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.prodstockmgr.parameter.GetInventoryCheckingListBean;
import com.shop2cn.iapi.prodstockmgr.service.GetInventoryCheckingListCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.DetailsCatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.model.godmch.mch_depot_info;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;

/**
 * 库存盘点清单Test FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetInventoryCheckingList {
	private static GetInventoryCheckingListBean getinventorycheckinglistBean;
	private static GetInventoryCheckingListCall getinventorycheckinglistCall;
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	private static int csId = AgentUsers.csId;
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	private static int csId1 = AgentUsers.csId1;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("库存盘点清单Test");
		try {
			GodfreightmgrService.deleteBySellerId(rootUserId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		getinventorycheckinglistBean = new GetInventoryCheckingListBean();
		getinventorycheckinglistCall = new GetInventoryCheckingListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getinventorycheckinglistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(JsonArray catalogList,String productId) throws Exception {
		Logger.comment("数据库验证");
		ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
		List<DetailsCatalogDto> catalogs = productDetailsDto.getCatalogList();
		catalogList.forEach(b->{
			JsonObject catalogj = b.getAsJsonObject();
			for (DetailsCatalogDto catalog : catalogs) {
				if(catalogj.get("skuId").getAsString().equals(catalog.getSku())) {
					Logger.verifyEquals(productDetailsDto.getPics().get(0), catalogj.get("mainPic").getAsString(),"验证mainPic");
					Logger.verifyEquals(productDetailsDto.getTitle(), catalogj.get("productName").getAsString(),"验证productName");
					Logger.verifyEquals(catalog.getSku(), catalogj.get("skuId").getAsString(),"验证skuId");
					Logger.verifyEquals(catalog.getPropertyValue(), catalogj.get("propertyValue").getAsString(),"验证propertyValue");
					Logger.verifyEquals(catalog.getRealStock(), catalogj.get("realStock").getAsInt(),"验证realStock");
					Logger.verifyEquals(catalog.getVirtualStock(), catalogj.get("virtualStock").getAsInt(),"验证virtualStock");
				}
				
			}
		});
		
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_001() {
		Logger.start(true, "根据商品名称进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			Thread.sleep(2000);
			getinventorycheckinglistBean.setProductName(addAgentProductBean.getTitle());
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");

			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_002() {
		Logger.start(true, "根据skuID进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			Thread.sleep(2000);
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistBean.setSkuId(addAgentProductBean.getCatalogList().get(0).getSku());

			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_003() {
		Logger.start(true, "根据商品名称+sku进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			Thread.sleep(2000);
			getinventorycheckinglistBean.setSkuId(addAgentProductBean.getCatalogList().get(0).getSku());
			getinventorycheckinglistBean.setProductName(addAgentProductBean.getTitle());
			getinventorycheckinglistBean.setSellerId(rootUserId);

			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_004() {
		Logger.start(true, "根据真实库存的区间进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(905);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistBean.setMinStock(1034);
			getinventorycheckinglistBean.setMaxStock(1036);

			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");

			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_005() {
		Logger.start(true, "没有条件，查询全部");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");

			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_006() {
		Logger.start(true, "根据品牌Id进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistBean.setBrandId(addAgentProductBean.getBrandId());
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");

			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_007() {
		Logger.start(true, "根据二级或三级类目id进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistBean.setCategoryId(addAgentProductBean.getCategoryId());
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");

			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_008() {
		Logger.start(true, "根据备注进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String remark="备注"+TestDataManager.getRandomNum(6);
			addAgentProductBean.setRemark(remark);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);

			Thread.sleep(2000);
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistBean.setRemark(remark);
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");

			
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_009() {
		Logger.start(true, "根据仓库进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			List<mch_depot_info> Depots = GodmchManageCallService
					.SelectDepotByMchIdAndSuccess(rootUserId);
					addAgentProductBean.setDepotId(Depots.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistBean.setDepotId(Depots.get(0).getDepotId());
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
			YmtProductsI ymtProductsI = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
			Logger.verifyEquals(ymtProductsI.getiBondedArea(), Depots.get(0).getDepotId(), "验证仓库");
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_010() {
		Logger.start(true, "根据品牌Id进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistBean.setBrandId(addAgentProductBean.getBrandId());
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_011() {
		Logger.start(true, "根据二级或三级类目id进行查询");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			getinventorycheckinglistBean.setCategoryId(addAgentProductBean.getCategoryId());
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_012() {
		Logger.start(true, "根据物流类型查询");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			getinventorycheckinglistBean.setDeliveryTypes(Arrays.asList(999));
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			verifyDB(catalogList,productId);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetInventoryCheckingList_013() {
		Logger.start(true, "查询与返回增加条码");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			List<String> barCodes = Arrays.asList("hellohi"+TestDataManager.randomStr(10),"hihello"+TestDataManager.randomStr(10));
			addAgentProductBean.getCatalogList().get(0).setBarcode(barCodes.get(0));;
			addAgentProductBean.getCatalogList().get(1).setBarcode(barCodes.get(1));;
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			long orderId = Shop2cnStockCallService.stockOutOnDelivery(rootUserId, productId, 10);
			getinventorycheckinglistBean.setDeliveryTypes(Arrays.asList(999));
			getinventorycheckinglistBean.setBarcodes(barCodes.get(0)+";"+barCodes.get(1));
			getinventorycheckinglistBean.setSellerId(rootUserId);
			getinventorycheckinglistCall.setData(getinventorycheckinglistBean);
			getinventorycheckinglistCall.callService();
			Logger.verifyEquals("200", getinventorycheckinglistCall.getString("code"), "验证code");
			JsonArray catalogList = getinventorycheckinglistCall.getJsonObject().getAsJsonObject("data").getAsJsonArray("catalogList");
			Logger.verifyEquals(catalogList.get(0).getAsJsonObject().get("barcode").getAsString(), barCodes.get(0), "验证条形码");
			Logger.verifyEquals(catalogList.get(1).getAsJsonObject().get("barcode").getAsString(), barCodes.get(1), "验证条形码");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
}